package com.qzsoft.zhjd.machine.modules.machinefuel.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.machine.modules.machinefuel.dto.MachineRefuelRecordsDto;
import com.qzsoft.zhjd.machine.modules.machinefuel.entity.MachineRefuelRecordsEntity;
import com.qzsoft.zhjd.machine.modules.machinefuel.mapper.MachineRefuelRecordsMapper;
import com.qzsoft.zhjd.machine.modules.machinefuel.service.MachineRefuelRecordsService;

/**
 * @ClassName MachineRefuelRecordsServiceImpl
 * @Description 加油记录业务接口实现
 * @author linhuawei
 * @since 2021-11-04
 */
@Service
public class MachineRefuelRecordsServiceImpl extends ServiceImpl<MachineRefuelRecordsMapper, MachineRefuelRecordsEntity>
        implements MachineRefuelRecordsService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    public IPage<MachineRefuelRecordsDto> pageDto(Map<String, Object> params) {
        IPage<MachineRefuelRecordsDto> page = baseServiceComponent.getPage(params, MachineRefuelRecordsDto.class);
        page = baseMapper.queryDto(page, params);
        return ConvertDictUtils.formatDicDataPage(page);
    }

    @Override
    public IPage<MachineRefuelRecordsEntity> pageForMachine(String machineId, Map<String, Object> params) {
        // 设置查询条件
        QueryWrapper<MachineRefuelRecordsEntity> queryWrapper = new QueryWrapper<MachineRefuelRecordsEntity>();
        queryWrapper.eq("machine_id", machineId);
        if(ObjectUtils.isNotEmpty(params.get("startTime"))) {
            queryWrapper.ge("DATE_FORMAT( collect_date, '%Y-%m-%d' )", params.get("startTime"));
        }
        if(ObjectUtils.isNotEmpty(params.get("endTime"))) {
            queryWrapper.le("DATE_FORMAT( collect_date, '%Y-%m-%d' )", params.get("endTime"));
        }
        queryWrapper.orderByDesc("collect_date");
        
        // 设置分页参数
        IPage<MachineRefuelRecordsEntity> page = baseServiceComponent.getPage(params, MachineRefuelRecordsEntity.class);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean updateById(MachineRefuelRecordsEntity entity) {
        entity.setCustomDate(new Date());// 设置校正日期
        entity.setVolume(entity.getCustomVolume());// 使用校正值作为加油值
        return super.updateById(entity);
    }

}
